package org.wmq.organizationmanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.wmq.organizationmanagement.mapper.OrganizationMasterMapper;
import org.wmq.organizationmanagement.model.po.Member;
import org.wmq.organizationmanagement.model.po.OrganizationMaster;
import org.wmq.organizationmanagement.model.vo.MemberVO;
import org.wmq.organizationmanagement.service.OrganizationMasterService;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class OrganizationMasterServiceImpl extends ServiceImpl<OrganizationMasterMapper, OrganizationMaster> implements OrganizationMasterService {
    private final OrganizationMasterMapper organizationMasterMapper;

    @Override
    public List<MemberVO> getListByOrganizationId(Long organizationId) {
        List<Member> memberList = this.organizationMasterMapper.selectListByOrganizationId(organizationId);
        if (memberList == null){
            log.error("该组织没有任何组织者");
            return null;
        }
        return memberList.stream()
                .map(member -> {
                    MemberVO memberVO = new MemberVO();
                    BeanUtils.copyProperties(member,memberVO);
                    return memberVO;
                }).toList();
    }

    @Override
    public List<MemberVO> getListByOrganizationIdAndMasterName(Long organizationId, String masterName) {
        List<Member> memberList = this.organizationMasterMapper.selectListByOrganizationIdAndMasterName(organizationId, masterName);
        if (memberList == null){
            log.error("该组织没有任何同名组织者");
            return null;
        }
        return memberList.stream()
                .map(member -> {
                    MemberVO memberVO = new MemberVO();
                    BeanUtils.copyProperties(member,memberVO);
                    return memberVO;
                }).toList();
    }

    @Override
    public boolean removeByOrganizationId(Long organizationId) {
        return this.lambdaUpdate().eq(OrganizationMaster::getOrganizationId, organizationId).remove();
    }

    @Override
    public boolean removeByOrganizationIdAndMasterId(Long organizationId, Long masterId) {
        return this.lambdaUpdate().eq(OrganizationMaster::getOrganizationId, organizationId)
                .eq(OrganizationMaster::getMasterId, masterId).remove();
    }

    @Override
    public boolean removeByMasterId(Long masterId) {
        return this.lambdaUpdate().eq(OrganizationMaster::getMasterId, masterId).remove();
    }
}
